package com.muxi.association.interceptor;

import com.muxi.association.pojo.entity.AssocMember;
import com.muxi.association.pojo.entity.Role;
import com.muxi.association.pojo.entity.User;
import com.muxi.association.service.AssocMemberService;
import com.muxi.association.service.RoleService;
import com.muxi.association.util.ParameterDealUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ZXW
 * 社团删除权限拦截
 */

@Slf4j
@Component
public class AssociationInterceptor implements HandlerInterceptor {

    @Resource
    private AssocMemberService assocMemberService;

    @Resource
    private RoleService roleService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String idStr = request.getParameter("idStr");
        // 编辑id
        String id = request.getParameter("id");
        // 删除id数组
        List<Integer> ids = ParameterDealUtil.stringSplitArray(idStr);
        System.out.println(id);
        if (null != id) {
            ids = new ArrayList<>();
            ids.add(Integer.parseInt(id));
        }
        if (null != ids) {
            System.out.println(ids);
            for (int i : ids) {
                AssocMember assocAdmin = assocMemberService.getAssocMemberByUserIdAssociationId(user.getId(), assocMemberService.getAssocMemberOne(i).getAssociationId());
                if (assocAdmin != null) {
                    Role roleOne = roleService.getRoleOne(assocAdmin.getRoleId());
                    if (roleOne == null || !"社长".equals(roleOne.getRole())) {
                        returnJson(response,"{\"code\":401,\"msg\":\"权限不足\"}");
                        return false;
                    }
                } else {
                    returnJson(response,"{\"code\":401,\"msg\":\"权限不足\"}");
                    return false;
                }
            }
            return true;
        }
        returnJson(response,"{\"code\":401,\"msg\":\"权限不足\"}");
        return false;
    }

    /**
     * 返回客户端数据
     * @param response
     * @param json
     * @throws Exception
     */
    private void returnJson(HttpServletResponse response, String json) {
        PrintWriter writer = null;
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(json);

        } catch (IOException e) {
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}
